package com.pfzy.ui.activity.dragsort

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemDragListener
import com.chad.library.adapter.base.listener.OnItemLongClickListener
import com.chad.library.adapter.base.module.BaseDraggableModule
import com.pfzy.ui.R
import com.pfzy.ui.databinding.ActivityDragSortBinding

// https://www.jianshu.com/p/a9e64fd7bd75
// https://blog.csdn.net/weixin_42061754/article/details/129159322
// https://juejin.cn/post/6844903439143550984?searchId=20231107104915A05545640EC3BD78B835
class DragSortActivity : AppCompatActivity() {
    private lateinit var mBind: ActivityDragSortBinding

    //    private var mDragSortAdapter: DragSortAdapter? = null
    private var mDragSortList: ArrayList<DragSortEntity> = ArrayList()
    private var mItemTouchHelper: ItemTouchHelper? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBind = ActivityDragSortBinding.inflate(layoutInflater)
        setContentView(mBind.root)
        initView()
    }

    private fun initView() {
        mDragSortList.add(DragSortEntity(this.resources.getDrawable(R.mipmap.airport), "接送机/站"))
        mDragSortList.add(DragSortEntity(this.resources.getDrawable(R.mipmap.intercitybus), "城际出行"))
        mDragSortList.add(DragSortEntity(this.resources.getDrawable(R.mipmap.theway), "顺风车"))
        mDragSortList.add(DragSortEntity(this.resources.getDrawable(R.mipmap.ridehail), "同城快车"))

        setDragSortAdapter1()
        setDragSortAdapter2()


//        mDragSortAdapter!!.setOnItemLongClickListener(object : OnItemLongClickListener {
//            override fun onItemLongClick(
//                adapter: BaseQuickAdapter<*, *>,
//                view: View,
//                position: Int
//            ): Boolean {
//                return true// true截断消息
//            }
//        })
//        mBind.recyclerEditableRv.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))// 线


//        incomeAdapter!!.setOnItemClickListener { view, position ->
//            // 添加拖拽事件
//            // longClickPosition：初始索引值
//            // ActionUpPosition：结束索引值
//            mItemTouchHelper = ItemTouchHelper(MyItemTouchHelper(RecyclerEditableActivity(), infoList, incomeAdapter, label@ MyItemTouchHelper.RequestDouble { longClickPosition: Int, ActionUpPosition: Int? ->
//                    if (longClickPosition == -1)
//                    //用swap方法可以交换集合中任意两个元素的位置
////                    Collections.swap(infoList, longClickPosition, ActionUpPosition!!)
//
//                    //列表的适配器
//                        incomeAdapter!!.notifyDataSetChanged()
//                }))
//        }
//
//        mItemTouchHelper = ItemTouchHelper(ItemTouchHelperCallback())
//        mItemTouchHelper!!.attachToRecyclerView(mBind.recyclerEditableRv)
    }

    private fun setDragSortAdapter1() {
        var mDragSortAdapter = DragSortAdapter(mDragSortList)
        val draggableModule: BaseDraggableModule = mDragSortAdapter!!.draggableModule!!// 创建适配器.拖拽
        draggableModule.isDragEnabled = true //启动拖拽
        draggableModule.itemTouchHelperCallback.setDragMoveFlags(ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT or ItemTouchHelper.UP or ItemTouchHelper.DOWN) //拖拽方向
        draggableModule.setOnItemDragListener(object : OnItemDragListener {
            // 拖拽监听
            override fun onItemDragStart(viewHolder: RecyclerView.ViewHolder, pos: Int) {
                Toast.makeText(
                    this@DragSortActivity,
                    "onItemDragBefore" + viewHolder.layoutPosition,
                    Toast.LENGTH_SHORT
                ).show()
            }

            override fun onItemDragMoving(
                source: RecyclerView.ViewHolder,
                from: Int,
                target: RecyclerView.ViewHolder,
                to: Int
            ) {

            }

            override fun onItemDragEnd(viewHolder: RecyclerView.ViewHolder, pos: Int) {
                Toast.makeText(
                    this@DragSortActivity,
                    "onItemDragAfter" + viewHolder.layoutPosition,
                    Toast.LENGTH_SHORT
                ).show()
            }
        })
        mBind.actDragSortRlv1.layoutManager = LinearLayoutManager(this)
        mBind.actDragSortRlv1.layoutManager =
            LinearLayoutManager(this, RecyclerView.HORIZONTAL, false)
        mBind.actDragSortRlv1.adapter = mDragSortAdapter
    }

    private fun setDragSortAdapter2() {
//        var list: ArrayList<DragSortEntity> = ArrayList()
//        list.addAll(mDragSortList)
        var mDragSortAdapter = DragSortAdapter(mDragSortList)
        mDragSortAdapter.setOnItemLongClickListener(object : OnItemLongClickListener {
            override fun onItemLongClick(
                adapter: BaseQuickAdapter<*, *>,
                view: View,
                position: Int
            ): Boolean {
                // 改变数据状态
//                var draggableBean = adapter.data[position]
                return false;
            }
        })
        mBind.actDragSortRlv2.layoutManager = LinearLayoutManager(this)
        mBind.actDragSortRlv2.layoutManager =
            LinearLayoutManager(this, RecyclerView.HORIZONTAL, false)
        mBind.actDragSortRlv2.adapter = mDragSortAdapter
        AdapterTouchHelper2(
            mBind.actDragSortRlv2,
            mDragSortAdapter
        )
    }
}
































